

var moment = require("moment");
var iz = require('iz');
var math = require("mathjs");

const clone = require('clone');

exports.firstUpperCase = function (str) {  //首字母大写
    return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
}
exports.fmtCustomerOrder = function (order) {
    order.flow = this.IsNullThen(order.flow, {});
    order.flow.proof = this.IsNullThen(order.flow.proof, {});
    order.flow.bulk = this.IsNullThen(order.flow.bulk, {});
    order.colorArray = this.IsNullThen(order.colorArray, {});



    order.flow.proof.nodeList = fmtNodeList(order.flow.proof.nodeList);  //打样流程
    order.flow.bulk.nodeList = fmtNodeList(order.flow.bulk.nodeList);    //大货流程
    return order;
}
var fmtNodeList = (nodeList) => {

    nodeList = this.IsNullThen(nodeList, []);

    nodeList.forEach(function (node) {
        node.time = this.IsNullThen(node.time, []);

        node.statusId = this.IsNullThen(node.statusId, 0);   //0未执行   100 执行中  200 执行完成


        node.time.forEach(function (tm, index) {

            if (iz.string(tm)) {
                node.time[index] = moment(tm)._d;
            }
        }, this);

    }, this);
    return nodeList;
}

exports.IsNullThen = function (obj, obj2, obj3) {

    if (!obj) {

        obj = obj2
    }
    else {
        //如果有

        if (obj3) {

            obj = obj3
        }

    }





    return obj;

}

exports.isString = function (obj) {
    // body...
    return iz.string(obj);
};

exports.isArray = function (obj) {
    // body...

    if (!obj) {
        return false
    }
    try {
        return iz.anArray(obj);
    } catch (e) {
        return false
    }

};
function RndNum(n) {
    var rnd = "";
    for (var i = 0; i < n; i++)
        rnd += Math.floor(Math.random() * 10);
    return rnd;
}

exports.getId = function () {

    // body...
    return moment().utcOffset(-8).format("YYMMDDHHmmssSSS") + RndNum(4);


};

exports.fileExt = function (url) {
    // body...
    var fileExtension = url.substring(url.lastIndexOf('.') + 1);
    return fileExtension.toLowerCase()

};

exports.isImg = function (url) {
    // body...
    var ext = this.fileExt(url);

    switch (ext) {
        case "jpg":
        case "jpeg":
        case "png":
        case "gif":
        case "tif":
        case "bmp":
            return true


        default:
            return false

    }

};



exports.isNumber = function (obj) {
    // body...
    return iz.number(obj);
};
exports.isDate = function (obj) {
    // body...// Is a date obj or is a string that is easily converted to a date
    return iz.date(obj);
};


exports.Percentage = function (number1, number2) {
    // body...

    var n = (Math.round(number1 / number2 * 10000) / 100.00);// 小数点后两位百分比
    return n
};


exports.now = function () {
    var createTime = moment().utcOffset(-8)._d;
    return createTime
};


exports.clone = function (obj) {
    var newObj = clone(obj)

    return newObj
};